package Demo5;


class MyBlockingQueue{
    public String[] data=null;
    private int head=0;
    private int tail=0;
    private int size=0;

    public MyBlockingQueue(int capacity) {
        data=new String[capacity];
    }

    public void put(String s) throws InterruptedException {
        synchronized (this){
            while(size>= data.length){
                this.wait();
            }
            data[tail]=s;
            size++;
            tail++;
            if(tail>=size){
                tail=0;
            }
            this.notify();
        }
    }

    public String take() throws InterruptedException {
        synchronized (this){
            while(size==0){
                this.wait();
            }
            String s1=data[head];
            head++;
            if(head>= data.length){
                head=0;
            }
            size--;
            this.notify();
            return s1;
        }
    }
}

public class Main11 {
    public static void main(String[] args) {
MyBlockingQueue queue=new MyBlockingQueue(100);
Thread t1=new Thread(()->{
   while(true){
       try {
           queue.put("aaaaa");
           System.out.println("入队元素：aaaaa");
       } catch (InterruptedException e) {
           throw new RuntimeException(e);
       }
   }
});
        Thread t2=new Thread(()->{
            while(true){
                try {
                   String s= queue.take();
                    System.out.println("出队元素："+s);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
